package e3dcs

import (
	"fmt"

	jsoniter "github.com/json-iterator/go"
	"github.com/pkg/errors"
)

// meta 元数据
type meta struct {
	ServiceID string    `json:"srv_id,omitempty"`
	BLMode    BLMode    `json:"bl_mode,omitempty"`
	State     stateKind `json:"state,omitempty"`
	SessionID string    `json:"ssid,omitempty"`
	RpcAddr   string    `json:"rpc_addr,omitempty"`
}

func (ts *meta) E3LogMarshall() string {
	return fmt.Sprintf(`{"srv_id:%s","ssid":%s}`, ts.ServiceID, ts.SessionID)
}

func (ts *meta) errorWarp(err error, msg string) error {
	if err == nil {
		return errors.New("meta service -> " + msg)
	}
	return errors.WithMessage(err, "meta service -> "+msg)
}

func (ts *meta) equal(other *meta) bool {
	return ts.ServiceID == other.ServiceID && ts.SessionID == other.SessionID
}

func (ts *meta) toJson() string {
	out, _ := jsoniter.MarshalToString(ts)
	return out
}

func (ts *meta) fromJson(js string) error {
	err := jsoniter.UnmarshalFromString(js, ts)
	if err != nil {
		return ts.errorWarp(err, "from json:"+js)
	}
	return nil
}

type moveCnt struct {
	Cnt  int
	Cost float64
}

func (ts moveCnt) String() string {
	return fmt.Sprintf("{cnt:%d, cost:%.2f}", ts.Cnt, ts.Cost)
}
